package icasue.reflect.handles.integer;

import icasue.reflect.handles.OAble;
import lombok.SneakyThrows;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/12/2 上午10:59
 * @UpdateDate:
 * @Description:
 */
public class IntegerO implements OAble {

    static {
        try {
            Class<?> loadHandleOperate = IntegerO.class.getClassLoader().loadClass("icasue.reflect.handles.HandleSupplier");
            IntegerO.lookup = (MethodHandles.Lookup)loadHandleOperate.getDeclaredField("methodLookup").get(null);
        }catch (Throwable e){
            throw new RuntimeException("ObjectO init error.",e);
        }
    }

    static {
        findHandles();
    }

    public static MethodHandles.Lookup lookup;

    //base operator.
    public static MethodHandle compare;
    public static MethodHandle max;
    public static MethodHandle min;
    public static MethodHandle valueOf;


    @SneakyThrows(Throwable.class)
    private static void findHandles() {
        compare = lookup.findStatic(Integer.class, "compare", MethodType.methodType(int.class, int.class, int.class));
        max = lookup.findStatic(Integer.class, "max", MethodType.methodType(int.class, int.class, int.class));
        min = lookup.findStatic(Integer.class, "min", MethodType.methodType(int.class, int.class, int.class));
        valueOf = lookup.findStatic(Integer.class, "valueOf", MethodType.methodType(Integer.class, String.class));
    }
}
